package main

import (
	"context"
	"fmt"
	"time"

	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

func main() {
	var (
		client     *mongo.Client
		err        error
		opt        *options.ClientOptions
		database   *mongo.Database
		collection *mongo.Collection
	)

	// 1.建立连接
	opt = options.Client() // 生成一个Clientopt
	opt.ApplyURI("mongodb://127.0.0.1:27017")
	opt.SetConnectTimeout(5 * time.Second)

	if client, err = mongo.Connect(context.TODO(), opt); err != nil {
		fmt.Println(err)
		return
	}

	// 2.选择数据库my_db
	database = client.Database("my_db")

	// 3.选择表my_collection
	collection = database.Collection("my_collection")

	collection = collection
}
